
from django.contrib.auth import get_user_model
from django.shortcuts import render
# Create your views here.
import random
# 获取短信验证码
from django_redis import get_redis_connection
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from user.serializers import RegisterSerializer
from user.models import User
# User = get_user_model()




class Smscodeview(APIView):
    def get(self,request,mobile):
        # 链接redis数据库
        redis_conn = get_redis_connection('verify')
        # 避免频繁发送短信
        sms_flag = redis_conn.get('flag_%s' % mobile)
        if sms_flag:
            return Response({'message':'请不要频繁发送短信'})
        else:
            send_code = '%06d'% random.randint(0, 999999)
            print(send_code)
            # 管道管理redis
            pl = redis_conn.pipeline()
            pl.setex('flag_%s'%mobile,60,1)
            pl.setex('send_code_%s'%mobile,300,send_code)
            pl.execute()
            # 使用celery异步发送短信
            # send_sms_code_func.delay(mobile, sms_code)
            return Response({"success":True,
                             "sms_code":send_code,
                             "message":"ok"})